package two_shang._12_03;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class TwoTxt {
    public static void main(String[] args) {
        //创建文件的时候就可以指定路径
        String oneFileName = "X:\\IntelliJ IDEA 2023.1.2\\project\\ClassTest\\src\\two_shang._12_03\\File\\one.txt";
        String twoFileName = "X:\\IntelliJ IDEA 2023.1.2\\project\\ClassTest\\src\\two_shang._12_03\\File\\two.txt";
        int zishu = 10;

        // 写入文件one.txt
        long startTimeOne = System.currentTimeMillis();
        try (FileOutputStream fos = new FileOutputStream(oneFileName)) {
            for (int i = 0; i < zishu; i++) {
                fos.write('a');
            }
        } catch (IOException e) {
            System.out.println("写入文件one.txt时出现错误：" + e.getMessage());
        }
        long endTimeOne = System.currentTimeMillis();
        long totalTimeOne = endTimeOne - startTimeOne;

        // 写入文件two.txt(缓冲流文件)
        long startTimeTwo = System.currentTimeMillis();
        try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(Paths.get(twoFileName)))) {
            for (int i = 0; i < zishu; i++) {
                bos.write('b');
            }
        } catch (IOException e) {
            System.out.println("写入文件two.txt时出现错误：" + e.getMessage());
        }
        long endTimeTwo = System.currentTimeMillis();
        long totalTimeTwo = endTimeTwo - startTimeTwo;

        System.out.println("one . txt不使用缓冲流来写\n" +
                "用时为: " + totalTimeOne + " 毫秒");
        System.out.println("two. txt使用缓冲流来写\n" +
                "用时为: " + totalTimeTwo + " 毫秒");
        System.out.println("节省时间:" + (totalTimeOne - totalTimeTwo));

    }
}
